﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcmyjobs.Web.enprise
{
    public partial class ereset_pwd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["email"] != null)
                {
                    string email = Request.Params["email"];

                    lcmyjobs.BLL.enterprise_basic eb = new BLL.enterprise_basic();

                    // 取得用户ID
                    DataSet ds = eb.FindIDByEmail(email);

                    // 验证ID
                    if (ds != null && ds.Tables[0].Rows.Count != 0)
                    {
                        int dtNow = int.Parse(getUnixTime(DateTime.Now));
                        int dtSave = int.Parse((ds.Tables[0].Rows[0]["resetpwdtime"]).ToString());

                        // 验证是否超时 
                        if (dtNow - dtSave < 3600 * 24 * 2)
                        {
                            // 在隐藏区域和session中保留用户ID
                            this.HF_ID.Value = ds.Tables[0].Rows[0]["enterprise_bid"].ToString();
                            Session["reset_id"] = ds.Tables[0].Rows[0]["enterprise_bid"].ToString();
                        }
                        else
                        {
                            Maticsoft.Common.MessageBox.Show(this, "链接过期！请重新发送验证邮件！");
                            Response.Redirect("eforget_pwd.html");
                        }

                    }
                    else
                    {
                        Maticsoft.Common.MessageBox.Show(this, "邮箱不正确！请重新发送验证邮件！");
                        Response.Redirect("eforget_pwd.html");
                    }
                }
                else
                {
                    Maticsoft.Common.MessageBox.Show(this, "无效用户！请重新发送验证邮件！");
                    Response.Redirect("eforget_pwd.html");
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (Session["reset_id"] == null)
                {
                    Maticsoft.Common.MessageBox.Show(this, "输入超时！请重新发送验证邮件！");
                    Response.Redirect("eforget_pwd.html");
                }

                // 验证隐藏区域和session中保留的ID
                if (Session["reset_id"].ToString() == this.HF_ID.Value.ToString())
                {
                    lcmyjobs.BLL.enterprise_basic eb = new BLL.enterprise_basic();
                    DataSet ds = eb.getResetPWDTime(Session["reset_id"].ToString());

                    // 验证结果
                    if (ds != null && ds.Tables[0].Rows.Count != 0)
                    {

                         // 修改密码
                        int id = int.Parse(Session["reset_id"].ToString());
                        //string oldpwd = ds.Tables[0].Rows[0]["pwd"].ToString();
                        string newpwd = myPublic.GetMd5(this.txtpwd.Text);

                        bool i = eb.UpdatePwd(id,  newpwd);
                        if (i == true)
                        {
                            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "修改成功！请用新密码登录您的帐号！", "EnterpriseIndex.html");
                            //Maticsoft.Common.MessageBox.Show(this, "修改成功！");
                            //Response.Redirect("EnterpriseIndex.html");
                        }
                        else
                        {
                            Maticsoft.Common.MessageBox.Show(this, "修改失败！");
                        }
                    }
                }
                else
                {
                    Maticsoft.Common.MessageBox.Show(this, "输入超时！请重新发送验证邮件！");
                    Response.Redirect("eforget_pwd.html");
                }

                Session["reset_id"] = null;
            }
        }

        private string getUnixTime(DateTime dt)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            //DateTime dtNow = DateTime.Parse(DateTime.Now.ToString());
            TimeSpan toNow = dt.Subtract(dtStart);
            string timeStamp = toNow.Ticks.ToString();
            return timeStamp = timeStamp.Substring(0, timeStamp.Length - 7);
        }
    }
}